package com.jxs.android.weiget;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jxs.android.cmcc.R;
import com.jxs.android.cmcc.data.MenuInfo;
import com.jxs.android.util.BitmapUtil;

public class MenuPageAdapter extends BaseAdapter {

	private LayoutInflater inflater;
	private ArrayList<MenuInfo> menuInfos;
	private Context context;

	public MenuPageAdapter(Context context, ArrayList<MenuInfo> menuInfos) {
		this.menuInfos = menuInfos;
		this.context = context;
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return menuInfos.size();
	}

	@Override
	public Object getItem(int position) {
		return menuInfos.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = inflater.inflate(R.layout.menu_item_iv_tv, null);

			viewHolder.item_icon = (ImageView) convertView.findViewById(R.id.item_icon);
			viewHolder.item_tv = (TextView) convertView.findViewById(R.id.item_tv);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		MenuInfo info = menuInfos.get(position);
		viewHolder.item_icon.setImageBitmap(BitmapUtil.getImageFromAssetsFile(context, "menupic/" + info.getMenu_pic_name()));
		viewHolder.item_tv.setText(info.getMenu_name());
		return convertView;
	}

	class ViewHolder {
		ImageView item_icon;
		TextView item_tv;
	}

}